package hu.myai.indicators.pivot;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The indicator returns the last higher high value. It return null in any other
 * cases. Noize is an internal parameter how many lower high is accepted in a
 * bullish period. Parameter gives the interval (before and after the timstamp
 * of the tick) where the pivot is a local pivot. Suggested value is 5
 * 
 * @author bozbalint
 * 
 * */
public class LastHigherHighIndicator extends AbstractIndicator {

	/**
	 * The object used when logging debug,errors,warnings and info.
	 * */
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	/**
	 * Value of the current highest tick
	 * */
	Double lastHigh = null;

	/**
	 * noize is the number of lower high that can be prior the new higher high
	 * */
	int noize = 2;
	int actualNoize;

	public LastHigherHighIndicator(Double parameter) {
		super(parameter);
	}

	String highID;

	/**
	 * @param noize
	 *            Noize is an internal parameter how many lower high is accepted
	 *            in a bullish period.
	 */
	public void setNoize(int noize) {
		this.noize = noize;
	}

	@Override
	public void init() {
		highID = register(new LastHighIndicator(parameter));
	}

	@Override
	protected IndicatorVO indicatorCalculation() {

		IndicatorVO highIndicator = indicatorMap.get(highID);
		if (highIndicator == null || !highIndicator.isChanged())
			return null;

		if (lastHigh == null) {
			lastHigh = highIndicator.getValue();
			return null;
		}

		// to check indicator change
		boolean isNewHigh = false;
		// check if the new tick is higher than the current high
		if (lastHigh < highIndicator.getValue()) {
			lastHigh = highIndicator.getValue();
			actualNoize = noize;
			isNewHigh = true;
		} else if (actualNoize > 0) {
//			lastHigh = highIndicator.getValue();
			actualNoize--;
			return null;
		} else {
			lastHigh = highIndicator.getValue();
			return null;
		}

		IndicatorVO indicator = new IndicatorVO();
		indicator.setDate(lastTick.getTime());
		indicator.setChanged(isNewHigh);
		indicator.setValue(lastHigh);

		log.debug(indicator.toString());
		return indicator;
	}	
}
